<template>
  <ContentContainer
    :show-pagination="false"
    :show-footer="false"
    :refresh="false">
    <div class="weight-label">搜索引擎爬虫限制</div>
    <div class="sub-label mb-6">限制搜索引擎对页面的索引和跟踪。</div>
    <a-switch
      v-if="data?.search_engine?.robot_deny_status"
      :model-value="form.search_engine.robot_deny_status"
      :checked-value="1"
      :unchecked-value="2"
      size="small"
      class="mb-6"
      :before-change="(val: number) => handleBeforeChange(val)"></a-switch>
  </ContentContainer>
</template>
<script lang="ts" setup>
import {
  getApiSiteSearchEngineApi,
  updateApiSiteSearchEngineApi
} from '@/apis/modules/site/site'

interface IForm {
  search_engine: {
    robot_deny_status: number
  }
}
const form = reactive<IForm>({
  search_engine: {
    robot_deny_status: 1
  }
})
const route = useRoute()
const id = computed(() => route.params.id as string)

const handleBeforeChange = (status: number) => {
  return updateApiSiteSearchEngineApi({
    id: id.value,
    search_engine: {
      robot_deny_status: status
    }
  })
    .then(() => {
      //ToastUtils.success('操作成功')
    })
    .finally(() => request({ id: id.value }))
}
//缓存配置
const { data, request } = useRequest(getApiSiteSearchEngineApi, {
  auto: false,
  params: { id: id.value },
  success: (res) => {
    Object.assign(form, res)
    if (!form?.search_engine?.robot_deny_status) {
      form.search_engine.robot_deny_status = 1
    }
  }
})
watchEffect(() => {
  request({ id: id.value })
})
</script>

<style lang="less" scoped>
.weight-label {
  @apply text-sm/22px font-700 text-1;
}
.sub-label {
  @apply text-xs/5 text-5;
}
</style>
